package com.example.weather;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.renderscript.ScriptGroup;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tvCity,tvTemp,tvWeather,tvWind;
    private Button btn_zheng,btn_beijing,btn_hang;
    private ImageView icon;
    private List<Map<String,String>> list;
    private Map<String,String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控件
        initView();
        //解析XML的信息/解析json

//        获取xml文件
//        InputStream inputStream = getResources().openRawResource(R.raw.weather);
//        获取json文件
        InputStream inputStream = this.getResources().openRawResource(R.raw.weather2);
        try {
//            用xml
//            List<WeatherInfo> weatherInfos = WeatherService.getInfoFromXml(inputStream);
//            用json
            List<WeatherInfo> weatherInfos = WeatherService.getInfoFromJson(inputStream);
            list = new ArrayList<Map<String,String>>();
            for(WeatherInfo info:weatherInfos){
                map = new HashMap<>();
                map.put("temp",info.getTemp());
                map.put("weather",info.getWeather());
                map.put("name",info.getName());
                map.put("wind",info.getWind());
                list.add(map);

            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        getMap(0,R.drawable.sun);

    }

    //将天气信息分条展示到界面上
    private void getMap(int i, int iconSource) {
        Map<String,String> cityMap = list.get(i);
        String temp = cityMap.get("temp");
        String weather = cityMap.get("weather");
        String name = cityMap.get("name");
        String wind = cityMap.get("wind");

        //给文本组件设置内容
        tvCity.setText(name);
        tvTemp.setText(temp);
        tvWeather.setText(weather);
        tvWind.setText("风力"+wind);
        //设置图片
        icon.setImageResource(iconSource);
    }

    private void initView() {
        //找到文本组件
        tvCity = findViewById(R.id.tv_city);
        tvTemp = findViewById(R.id.tv_temp);
        tvWeather = findViewById(R.id.tv_weather);
        tvWind = findViewById(R.id.tv_wind);
        //找到图片组件
        icon = findViewById(R.id.weather_img);
        //找到按钮组件
        btn_zheng = findViewById(R.id.btn_zheng);
        btn_beijing = findViewById(R.id.btn_beijing);
        btn_hang = findViewById(R.id.btn_hang);
        //给按钮设置点击事件
        btn_zheng.setOnClickListener(this);
        btn_beijing.setOnClickListener(this);
        btn_hang.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_zheng:
                getMap(0,R.drawable.sun);
                break;
            case R.id.btn_beijing:
                getMap(1,R.drawable.cloud);
                break;
            case R.id.btn_hang:
                getMap(2,R.drawable.rain);
                break;
        }

    }
}
